package com.aote.filter;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.w3c.dom.Element;

import com.browsesoft.Entity;
import com.browsesoft.EntityManager;
import com.browsesoft.htmlcomponent.HTMLBasicComponent;
import com.browsesoft.oa.LookFilter;
import com.browsesoft.user.User;

public class LinkedTableEntityFilter implements LookFilter {
	/**
	 * 用带数据的源table对应的entityID进行过滤
	 */
	public List filter(User user, HTMLBasicComponent component, List infors,
			HttpServletRequest request) {
		List result = new LinkedList();
		// 得到源table父组件所对应的entity的parentid
		String parentComponentParentID=(String) request.getParameter("parentid");
		// 读取源table的父组件entitytype
		Element element = component.getElement();
		String parentEntityType = element.getAttribute("parentEntityType");
		// 取源table的父组件的id，即table的parentid
		EntityManager manager = EntityManager.getInstance();
		Entity sourceEntity = (Entity) manager.getEntities(parentEntityType,
				"parentid", parentComponentParentID).getFirst();
		String sourceID = sourceEntity.getID();
		// 根据parentid对数据进行过滤
		Iterator iter = infors.iterator();
		while (iter.hasNext()) {
			Entity entity = (Entity) iter.next();
			String id=(String)entity.getAttributes().get("parentid");
			if (id.equals(sourceID)) {
				result.add(entity);
			}
		}
		return result;
	}
}
